package com.dq.controller.api.org;

import com.alibaba.fastjson.JSONObject;
import com.dq.annotation.aop.SysLog;
import com.dq.exception.customException.AdminException;
import com.dq.controller.in.user.OrgUserQuery;
import com.dq.controller.in.org.OrgQuery;
import com.dq.vo.ListDataVo;
import com.dq.vo.Vo;
import com.dq.domain.org.Org;
import com.dq.domain.sys.user.user.OrgUser;
import com.dq.service.AllService;
import com.dq.utils.BeanNullUtil;
import com.dq.utils.EditCheckUtil;
import com.dq.vo.baseVo.VO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ----
 */
@Api(description = "门店管理")
@RestController
@RequestMapping("/api/merchant/org/")
public class ApiOrgController extends AllService {
    @PostMapping("/orgList")
    @ApiOperation("门店列表-根据登录账号")
    public Vo orgList(@RequestBody OrgQuery orgQuery) throws AdminException {
        orgQuery.format();

        List<Org> list = orgService.listByLogin(orgQuery);
        return new ListDataVo((List<Org.Vo>) VO.toVOCollection(list), orgService.counteByLogin(orgQuery));
    }
    @PostMapping("/orgAdd")
    @ApiOperation("门店添加")
    public Vo orgAdd(@RequestBody Org orgAdd) throws AdminException {
        Org save = orgService.save(orgAdd);
        return new Vo(save.toVO());
    }
    @PostMapping("/orgEdit/{id}")
    @ApiOperation("门店信息修改")
    public Vo orgEdit(@PathVariable Long id, @RequestBody Org orgEdit) throws AdminException {
        orgEdit.setId(id);
        Org update = orgService.update(orgEdit);
        return new Vo(update.toVO());
    }

    @GetMapping("/orgDetail/{id}")
    @ApiOperation("门店详情")
    public Vo orgDetail(@PathVariable Long id) throws AdminException {
        Org org = dao.get(Org.class, id);
        if (null == org) {
            throw new AdminException("门店信息不存在");
        }
        return new Vo(org.toVO(false));
    }
    /**
     * 删除门店， 同时删除门店账号
     * @param id
     * @throws AdminException
     */
    @ApiOperation("删除")
    @SysLog("删除")
    @GetMapping("/orgDelete/{id}")
    public Vo orgDelete(@PathVariable @ApiParam(value = "id") Long id) throws AdminException {
        orgService.delete(id);

        return new Vo(HttpStatus.OK);
    }

    @PostMapping("/orgUserList")
    @ApiOperation("门店账号列表")
    public Vo orgAccountList(@RequestBody OrgUserQuery orgUserQuery) throws AdminException {
        orgUserQuery.format();
        Map<String, Object> query = orgUserService.getOrgListHql(orgUserQuery);

        ArrayList<JSONObject> list = new ArrayList<>();
        List<OrgUser> orgUserList = dao.find(dao.query2Hql(OrgUser.class, query), query);
        for (OrgUser orgUser : orgUserList) {
            list.add(orgUser.toBasicVO());
        }
        Long count = dao.count(dao.count2Hql(OrgUser.class, query), query);

        return new ListDataVo(list, count);
    }

    @PostMapping("/orgUserAdd/{orgId}")
    @ApiOperation("门店账号添加")
    public Vo orgAccountAdd(@ApiParam("门店ID") @PathVariable Long orgId, @RequestBody OrgUser orgUserAdd) throws AdminException {
        Org org = dao.get(Org.class, orgId);
        if (null == org) {
            throw new AdminException("门店信息不存在");
        }
        if(!EditCheckUtil.isMobileNO(orgUserAdd.getUsername()) || StringUtils.isBlank(orgUserAdd.getPassword())){throw new AdminException("手机号或密码格式不正确");}

        orgUserAdd.setSection(org);

        OrgUser save = orgUserService.save(orgUserAdd);
        return new Vo(save.toBasicVO());
    }

    @PostMapping("/orgUserEdit/{orgUserId}")
    @ApiOperation("门店用户信息/密码修改")
    public Vo orgAccountEdit(@ApiParam("账号ID") @PathVariable Long orgUserId, @RequestBody OrgUser orgUserEdit) throws AdminException {
        if(StringUtils.isNotBlank(orgUserEdit.getPassword())) {
            orgUserEdit.setPassword(sysUserService.getPassword(orgUserEdit.getPassword()));
        }
        OrgUser orgUser = dao.get(OrgUser.class,orgUserId);
        if (null == orgUser) {
            throw new AdminException("修改账号不存在");
        }
        BeanUtils.copyProperties(orgUserEdit, orgUser, BeanNullUtil.getNullPropertyNames(orgUserEdit));
        OrgUser update = orgUserService.update(orgUser);
        return new Vo(update.toBasicVO());
    }

    @GetMapping("/orgUserInfo/{orgUserId}")
    @ApiOperation("门店账号详情")
    public Vo orgAccountDetail(@ApiParam("账号ID") @PathVariable Long orgUserId) throws AdminException {
        OrgUser orgUser = dao.get(OrgUser.class, orgUserId);
        if (null == orgUser) {
            throw new AdminException("门店账号不存在");
        }
        return new Vo(orgUser.toBasicVO());
    }

    @ApiOperation("删除")
    @SysLog("删除")
    @PostMapping("/orgUserDelete/{orgUserId}")
    public Vo userDelete(@PathVariable @ApiParam("账号ID") Long orgUserId) throws AdminException {
        OrgUser orgUser = dao.get(OrgUser.class, orgUserId);
        if (null == orgUser) {
            throw new AdminException("门店账号不存在");
        }

        orgUser.setDeleted(true);

        orgUserService.update(orgUser);
        return new Vo(HttpStatus.OK);
    }

    /**
     * 获取 负责区域内 门店
     */
    @PostMapping("list")
    @ApiOperation("获取所有门店列表 - 不分页")
    public Vo getListByUser(@RequestBody OrgQuery orgQuery) throws AdminException {
        orgQuery.setPage(null);
        orgQuery.setSize(null);

        List<Org> list = orgService.list(orgQuery);
        return new ListDataVo((List<Org.Vo>) VO.toVOCollection(list));
    }
    /**
     * 获取 负责区域内 门店
     */
    @PostMapping("listPage")
    @ApiOperation("获取所有门店列表 - 分页")
    public Vo getListByUserPage(@RequestBody OrgQuery orgQuery) throws AdminException {

        List<Org> list = orgService.list(orgQuery);
        return new ListDataVo((List<Org.Vo>) VO.toVOCollection(list));
    }

}
